/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.MDoctor;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Munliga
 */
@Stateless
public class MDoctorFacade extends AbstractFacade<MDoctor> {
    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
    @EJB
    private MDoctorFacade mDoctorFacade;

    protected EntityManager getEntityManager() {
        return em;
    }

    public MDoctorFacade() {
        super(MDoctor.class);
    }

    //Create Query
    public List<MDoctor> LoadDataDoctor(String txtName, String txtSurname, String txtCode, String sortIndex, String sortDirection) {
        try {
            String strQuery = "SELECT m FROM MDoctor m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtName.isEmpty()) ? " AND m.name Like '%" + txtName + "%' " : " ";
            strWhere += (!txtSurname.isEmpty()) ? " AND m.surname Like '%" + txtSurname + "%' " : " ";
            strWhere += (!txtCode.isEmpty()) ? " AND m.code Like '%" + txtCode + "%' " : " ";
            strWhere += "ORDER BY m." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    public List<MDoctor> findByDoctorId(String id) {
        return em.createNamedQuery("MDoctor.findById").setParameter("id", Long.parseLong(id)).getResultList();
    }

    public void EditDoctor(String id, String txtName, String txtSurname, String rdbSex, String txtCode){
        MDoctor editDoctor = mDoctorFacade.find(Long.parseLong(id));
        editDoctor.setName(txtName);
        editDoctor.setSurname(txtSurname);
        editDoctor.setSex(Boolean.parseBoolean(rdbSex));
        editDoctor.setCode(txtCode);
        em.persist(editDoctor);
    }

    public void DeleteDoctor(String id){
        MDoctor deleteDoctor = mDoctorFacade.find(Long.parseLong(id));
        em.remove(deleteDoctor);
    }

    public void AddDoctor(String txtName, String txtSurname, String rdbSex, String txtCode){
        MDoctor addDoctor = new MDoctor();
        addDoctor.setName(txtName);
        addDoctor.setSurname(txtSurname);
        addDoctor.setSex(Boolean.parseBoolean(rdbSex));
        addDoctor.setCode(txtCode);
        em.persist(addDoctor);
    }
    public List<MDoctor> SearchDataDoctor(String txtKeyword) {
        try {
            String strQuery = "SELECT m FROM MDoctor m ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtKeyword.isEmpty()) ? " AND CONCAT(m.name,' ',m.surname,' ',m.code) Like '%" + txtKeyword + "%' " : " ";
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }
}
